package com.zgc.listener;

import com.zgc.cache.InformationTabCache;
import com.zgc.cache.TreeItemCache;
import com.zgc.constant.ZkConstant;
import com.zgc.view.controller.InformationTabCtrl;
import com.zgc.zkinfo.ZkConnectionInfo;
import com.zgc.zkinfo.ZkNodeInfo;
import javafx.scene.control.TreeItem;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.CuratorCacheListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CuratorTreeCacheListener implements CuratorCacheListener {
    protected static final Logger logger = LoggerFactory.getLogger(TreeItemCache.class);
    private final ZkConnectionInfo connectClient;

    public CuratorTreeCacheListener(ZkConnectionInfo connectClient) {
        this.connectClient = connectClient;
    }


    @Override
    public void event(Type type, ChildData oldData, ChildData data) {
        String id = connectClient.getId();
        logger.debug("{}监听,Type = {},ChildData = {},ChildData = {}", connectClient.getHost(), type, oldData, data);
        switch (type) {
            case NODE_CREATED -> {
                // 创建
                if (data != null) {
                    String path = data.getPath();
                    ZkNodeInfo zkNodeInfo = new ZkNodeInfo(id, path);
                    zkNodeInfo.setData(data.getData());
                    zkNodeInfo.setStat(data.getStat());

                    if (ZkConstant.ROOT_NODE.equals(path)) {
                        zkNodeInfo.setCurrentPath(ZkConstant.ROOT_NODE);
                    } else {
                        zkNodeInfo.setCurrentPath(path.substring(path.lastIndexOf(ZkConstant.ROOT_NODE) + 1));
                    }
                    // 如果节点存在就更新
                    if (TreeItemCache.containsTreeItem(id, path)) {
                        TreeItemCache.getTreeItem(id, path).setValue(zkNodeInfo);
                    } else {
                        // 如果节点不存在就创建
                        String parentPath = ZkConstant.ROOT_NODE;
                        if (path.split(ZkConstant.ROOT_NODE).length > 2) {
                            parentPath = path.substring(0, path.lastIndexOf(ZkConstant.ROOT_NODE));
                        }
                        if (TreeItemCache.containsTreeItem(id, parentPath)) {
                            TreeItem<ZkNodeInfo> treeItem = TreeItemCache.getTreeItem(id, parentPath);
                            TreeItem<ZkNodeInfo> zkNodeInfoTreeItem = new TreeItem<>(zkNodeInfo);
                            treeItem.getChildren().add(zkNodeInfoTreeItem);
                            TreeItemCache.put(id, data.getPath(), treeItem);
                        }
                    }
                }
            }
            case NODE_CHANGED -> {
                // 更新
                if (data != null) {
                    String path = data.getPath();
                    if (TreeItemCache.containsTreeItem(id, path)) {
                        ZkNodeInfo zkNodeInfo = TreeItemCache.getTreeItem(id, path).getValue();
                        zkNodeInfo.setStat(data.getStat());
                        zkNodeInfo.setData(data.getData());
                        InformationTabCtrl informationTabCtrl = InformationTabCache.getTab(id);
                        informationTabCtrl.setZkStat(zkNodeInfo);
                        informationTabCtrl.setZkData(zkNodeInfo);

                    }
                }
            }
            case NODE_DELETED -> {
                // 移除
                if (oldData != null) {
                    TreeItem<ZkNodeInfo> treeItem = TreeItemCache.getTreeItem(id, oldData.getPath());
                    treeItem.getParent().getChildren().remove(treeItem);
                    TreeItemCache.removeTreeItem(id, oldData.getPath());
                }
            }
        }


    }

}
